package it.softphone.cmc.server.entity;

import it.softphone.cmc.shared.entity.Attributes;
import it.softphone.cmc.shared.entity.TenantUser;
import it.softphone.commons.json.shared.JSONTransient;
import it.softphone.commons.service.LogAware;

import java.io.Serializable;
import java.util.Set;

import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

@Entity
public class UserEntity implements TenantUser,LogAware,Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2L;

	@Id @GeneratedValue(strategy = GenerationType.IDENTITY) 
	@JSONTransient private Key GUID;

	private String accountUID;
	@Basic private java.util.Set<String> permissions;	
	@Basic private java.util.Set<String> roles;	

	@Embedded 
	@OneToOne(cascade=CascadeType.ALL)
	private AttributesEntity attributes = new AttributesEntity();
	
	//@OneToOne(fetch=FetchType.EAGER,cascade=CascadeType.ALL,mappedBy="GUID")
	//TenantEntity theTenant;
	private String theTenant;
	
	private boolean enabled;

	private String id;
	private String name;
	
	public UserEntity() {
	}

	
	@Override
	public String toString() {
		return String
				.format("UserEntity [GUID=%s, accountUID=%s, permissions=%s, attributes=%s, theTenant=%s, enabled=%s, id=%s, name=%s]",
						GUID, accountUID, permissions, attributes, theTenant,
						enabled, id, name);
	}


	public final Key getGUID() {
		return GUID;
	}

	public final void setGUID(Key uID) {
		GUID = uID;
	}

	@Override
	public void setUID(String value) {
		if( value!=null ) setGUID( KeyFactory.stringToKey(value) );
	}


	@Override
	public String getUID() {
		if( getGUID()==null ) return null;
		return KeyFactory.keyToString(getGUID());
	}


	@Override
	public Attributes getAttributes() {
		return attributes;
	}


	@Override
	public void setAttributes(Attributes value) {
		attributes = (AttributesEntity) value;
		
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public it.softphone.commons.json.server.JSONObject encodeAttributes() {	
		return  attributes.encodeAttributes();
	}

	@Override
	public void decodeAttributes(Object object) {
		attributes.decodeAttributes(object);
	}

	@Override
	public void setAccountUID(String value) {
		accountUID = value;
	}

	@Override
	public String getAccountUID() {
		return accountUID;
	}

	@Override
	public String getTenant() {
		return theTenant;
	}

	@Override
	public void setTenant(String tenant) {
		theTenant = tenant;
	}

	@Override
	public Set<String> getPermissions() {
		if( permissions == null ) permissions = new java.util.HashSet<String>();
		return permissions;
	}

	@Override
	public void setPermissions(Set<String> permissions) {
		this.permissions = permissions;
	}

	@Override
	public boolean isEnabled() {
		return enabled;
	}

	@Override
	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	@Override
	public String getId() {
		return this.id;
	}

	@Override
	public void setId(String value) {
		this.id = value;
	}

	@Override
	public String getName() {
		return this.name;
	}

	@Override
	public void setName(String value) {
		this.name = value;
	}


	private String mail;
	private String phone;
	private String lastName;
	private String address;
	private String city;
	private String country;
	private String zipCode;
	
	@Override
	public String getMail() {
		return mail;
	}


	@Override
	public void setMail(String mail) {
		this.mail = mail;
	}


	@Override
	public String getPhone() {
		return phone;
	}


	@Override
	public void setPhone(String number) {
		this.phone = number;
	}


	@Override
	public String getLastName() {
		return lastName;
	}


	@Override
	public void setLastName(String value) {
		this.lastName = value;
	}


	@Override
	public String getAddress() {
		return address;
	}


	@Override
	public void setAddress(String value) {
		this.address = value;
	}


	@Override
	public String getCity() {
		return city;
	}


	@Override
	public void setCity(String value) {
		this.city = value;
	}


	@Override
	public String getCountry() {
		return country;
	}


	@Override
	public void setCountry(String value) {
		this.country = value;
	}


	@Override
	public String getZipCode() {
		return zipCode;
	}


	@Override
	public void setZipCode(String value) {
		this.zipCode = value;
	}

	@Override
	public void setRoles(java.util.Set<String> roles) {		
		this.roles = roles;
	}

	@Override
	public java.util.Set<String> getRoles() {
		if( null == roles ) roles = new java.util.HashSet<String>();
		return roles;
	}


}
